

// 取消打勾时 需要把已使用张数减-1
export function returnCheckedTicket(ticket, usedTickets, waresList) {
    const { ticketCode, useCondition, } = ticket
    // useCondition:"使用限制：0.无限制，1.桌台使用限制，2.会员使用限制"
    // 1.桌台使用限制 
    if (useCondition == 1) {
        usedTickets.forEach(item => {
            console.log('item.roomuse :>> ', item.roomuse);
            if (item.ticketCode == ticketCode) {
                item.roomuse -= 1
            }
        });
    }
    // 2.会员使用限制
    else if (useCondition == 2) {
        usedTickets.forEach(item => {
            if (item.ticketCode == ticketCode) {
                item.menberuse -= 1
            }
        });
    }
    // console.log('usedTickets :>> ', usedTickets);
}

// 验证桌台使用限制张数
export function table_use(ticket, usedTickets, waresList) {
    // console.log('ticket :>> ', ticket);
    // console.log('usedTickets :>> ', usedTickets[0]);
    // console.log('waresList :>> ', waresList[0]);
    let flag = true
    let errMsg = ''
    // useCondition:"使用限制：0.无限制，1.桌台使用限制，2.会员使用限制"
    const { ticketCode, useCondition, roomUseNum } = ticket
    if (useCondition != 1) return { flag, errMsg }
    usedTickets.forEach(item => {
        if (item.ticketCode == ticketCode) {
            // 可使用时张数+1 roomUseNum
            if (item.roomuse < roomUseNum) {
                item.roomuse += 1
                flag = true
            } else {
                flag = false
                errMsg = '超过可使用张数'
            }
        }
    });
    return { flag, errMsg }
}

// 验证会员使用限制张数
export function menber_use(ticket, usedTickets, waresList) {
    // console.log('ticket :>> ', ticket);
    // console.log('usedTickets :>> ', usedTickets[0]);
    // console.log('waresList :>> ', waresList[0]);
    let flag = true
    let errMsg = ''
    // useCondition:"使用限制：0.无限制，1.桌台使用限制，2.会员使用限制"
    const { ticketCode, useCondition, memberUseNum } = ticket
    if (useCondition != 2) return { flag, errMsg }
    usedTickets.forEach(item => {
        if (item.ticketCode == ticketCode) {
            // 可使用时张数+1
            if (item.menberuse < memberUseNum) {
                item.menberuse += 1
                flag = true
            } else {
                flag = false
                errMsg = '超过可使用张数'
            }
        }
    });
    return { flag, errMsg }
}

// 验证n个商品用的券不能超过n张
export function count_use(ticket, checkedList, waresList) {
    // console.log('ticket :>> ', ticket);
    // console.log('waresList :>> ', waresList);
    let flag = true
    let errMsg = ''
    // goodsCode   uomCode
    let waresCount = 0
    waresList.forEach(wares => {
        if (wares.goodsCode == ticket.goodsCode && wares.uomCode == ticket.uomCode) {
            waresCount += 1
        }
    });
    let checkedCount = 0
    checkedList.forEach(checked => {
        if (checked.goodsCode == ticket.goodsCode && checked.uomCode == ticket.uomCode) {
            checkedCount += 1
        }
    });
    // console.log('waresCount :>> ', waresCount, 'checkedCount :>> ', checkedCount);
    if (checkedCount < waresCount) return { flag, errMsg }
    flag = false
    errMsg = '选择数量大于商品数量'
    return { flag, errMsg }
}